#include "Base.h"
#include "BaseProtocol.h"

BOOL GetOriginName(int nOrigin, char *szOrigin, int nLen)
{
	switch(nOrigin)
	{
	case Origin_Game_Server:
		{
			strncpy(szOrigin,"GameServer", nLen - 1);
			return TRUE;
		}
	case Origin_Match_Server:
		{
			strncpy(szOrigin,"MatchServer", nLen - 1);
			return TRUE;
		}
	case Origin_Lobby_Server:
		{
			strncpy(szOrigin,"LobbyServer", nLen - 1);
			return TRUE;
		}
	case Origin_Database_Server:
		{
			strncpy(szOrigin,"DBServer", nLen - 1);
			return TRUE;
		}
	case Origin_MatchInfo_Server:
		{
			strncpy(szOrigin,"MatchInfoServer", nLen - 1);
			return TRUE;
		}
	case Origin_World_Server:
		{
			strncpy(szOrigin,"WorldServer", nLen - 1);
			return TRUE;
		}
	case Origin_Game_Client:
		{
			strncpy(szOrigin,"GameClient", nLen - 1);
			return TRUE;
		}
	default:
		return FALSE;
	}
}

int compArcNode(const void *pElement1,const void *pElement2)
{
	ArcNode *pNode1 = (ArcNode *)pElement1;
	ArcNode *pNode2 = (ArcNode *)pElement2;
	return pNode1->nOrigin - pNode2->nOrigin;
}

BOOL ClientConnectMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_nOrigin, sizeof(m_nOrigin));
	return TRUE;
}

BOOL ClientConnectMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_nOrigin, sizeof(m_nOrigin));
	return TRUE;
}

BOOL ServerConnectMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_nOrigin, sizeof(m_nOrigin));
	iStream.Read((char*)&m_nServerId, sizeof(m_nServerId));
	return TRUE;
}

BOOL ServerConnectMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_nOrigin, sizeof(m_nOrigin));
	oStream.Write((char*)&m_nServerId, sizeof(m_nServerId));
	return TRUE;
}

BOOL RegisterToManagerMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_nPort, sizeof(m_nPort));

	int nCount;
    iStream.Read((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		int nOrigin;
		iStream.Read((char*)&nOrigin, sizeof(nOrigin));
        m_listDstOrigin.AddTail(nOrigin);
	}
	return TRUE;
}

BOOL RegisterToManagerMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_nPort, sizeof(m_nPort));

	int nCount = m_listDstOrigin.GetCount();
	oStream.Write((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		int nOrigin = m_listDstOrigin.GetAt(i);
        oStream.Write((char*)&nOrigin, sizeof(nOrigin));
	}
	return TRUE;
}

BOOL ArchitectureMsg::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_nType, sizeof(m_nType));
	int nCount;
	iStream.Read((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		ArcNode node;
        iStream.Read((char*)&node, sizeof(ArcNode));
		m_listArcNode.Insert(node, compArcNode);
	}
	return TRUE;
}

BOOL ArchitectureMsg::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_nType, sizeof(m_nType));
	int nCount = m_listArcNode.GetCount();
	oStream.Write((char*)&nCount, sizeof(nCount));
	for(int i = 0; i < nCount; i++)
	{
		ArcNode &node = m_listArcNode.GetAt(i);
		oStream.Write((char*)&node, sizeof(ArcNode));
	}
	return TRUE;
}

BOOL ReqAdminLogin::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_szPassword, sizeof(m_szPassword));
	return TRUE;
}

BOOL ReqAdminLogin::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_szPassword, sizeof(m_szPassword));
	return TRUE;
}

BOOL AckAdminLogin::Read( CSocketStream& iStream )
{
	iStream.Read((char*)&m_bSuccess, sizeof(m_bSuccess));
	return TRUE;
}

BOOL AckAdminLogin::Write( CSocketStream& oStream ) const
{
	oStream.Write((char*)&m_bSuccess, sizeof(m_bSuccess));
	return TRUE;
}



